﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Assert;
using CommunitySystem.Ctrls;
using CommunitySystem.PublicOfficialSelectionService;
using CommunitySystem.Views;
using EHR.Common;

namespace CommunitySystem
{
    public partial class ShenPiControl3 : AsyncCtrl, IReturnListenerc
    {
        #region IReturnListenerc 成员
        public void BeginReturn()
        {
            //Load();
        }
        #endregion
        public NavMoveSwitch Nav { get; set; }
        private PublicOfficialSelectionServiceClient Client = new PublicOfficialSelectionServiceClient();
        public ObservableCollection<RenQianGongShi> models = new ObservableCollection<RenQianGongShi>();
        public int OrgID = 0;
        private string OrgName = "";
        public ShenPiControl3(int orgID, string orgName)
        {
            InitializeComponent();
            OrgID = orgID;
            OrgName = orgName;
            if(Session.LoginUser.LoginName == "admin")
            {
                OKButton.Visibility = System.Windows.Visibility.Collapsed;
                shenqing.Visibility = System.Windows.Visibility.Collapsed;
                queren.Visibility = System.Windows.Visibility.Collapsed;
                scrol.IsEnabled = false;
            }
            else
            {
                OKButton.Visibility = System.Windows.Visibility.Visible;
                shenqing.Visibility = System.Windows.Visibility.Visible;
                shenqing.IsEnabled = false;
                queren.Visibility = System.Windows.Visibility.Collapsed;
                scrol.IsEnabled = true;
            }
            Loaded();
        }

        public void Loaded()
        {
            Client.GetRenQianGongShiCompleted -= new EventHandler<GetRenQianGongShiCompletedEventArgs>(Client_GetRenQianGongShiCompleted);
            Client.GetRenQianGongShiCompleted += new EventHandler<GetRenQianGongShiCompletedEventArgs>(Client_GetRenQianGongShiCompleted);
            Client.GetRenQianGongShiAsync(OrgID);
        }

        void Client_GetRenQianGongShiCompleted(object sender, GetRenQianGongShiCompletedEventArgs e)
        {
            stackpanel.Children.Clear();
            if (e.Result != null && e.Result.Count > 0)
            {
                if (e.Result[0].ID != 0) shenqing.IsEnabled = true;
                if (e.Result[0].ID != 0 && Session.LoginUser.LoginName == "admin") queren.Visibility = System.Windows.Visibility.Visible;
                 OKButton.IsEnabled = true;
                models = e.Result;
                int count = ((ObservableCollection<RenQianGongShi>)e.Result).Count;
                for (int i = 0; i < count; i++)
                {
                    StackPanel s = new StackPanel();
                    s.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    s.Orientation = Orientation.Horizontal;

                    TextBlock tb1 = new TextBlock();
                    tb1.Text = "    经";
                    tb1.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    tb1.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    s.Children.Add(tb1);

                    TextBox t = new TextBox();
                    t.Tag = i;
                    t.TextChanged += new TextChangedEventHandler(TextBox_TextChanged);
                    if (!string.IsNullOrEmpty(e.Result[i].SNian)) t.Text = e.Result[i].SNian;
                    t.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    t.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    t.Width = 60;
                    t.Height = 25;
                    Binding b1 = new Binding();
                    b1.Source = e.Result[i];
                    b1.Path = new PropertyPath("SNian");
                    b1.Mode = BindingMode.TwoWay;
                    BindingOperations.SetBinding(t, TextBox.TextProperty, b1);
                    s.Children.Add(t);
                    
                    TextBlock tb2 = new TextBlock();
                    tb2.Text = "年";
                    tb2.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    tb2.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    s.Children.Add(tb2);

                    TextBox t1 = new TextBox();
                    t1.Tag = i;
                    t1.TextChanged += new TextChangedEventHandler(TextBox_TextChanged);
                    if (!string.IsNullOrEmpty(e.Result[i].SYUe)) t1.Text = e.Result[i].SYUe;
                    t1.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    t1.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    t1.Width = 60;
                    t1.Height = 25;
                    Binding b2 = new Binding();
                    b2.Source = e.Result[i];
                    b2.Path = new PropertyPath("SYUe");
                    b2.Mode = BindingMode.TwoWay;
                    BindingOperations.SetBinding(t1, TextBox.TextProperty, b2);
                    s.Children.Add(t1);
                    
                    TextBlock tb3 = new TextBlock();
                    tb3.Text = "月";
                    tb3.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    tb3.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    s.Children.Add(tb3);

                    TextBox t2 = new TextBox();
                    t2.Tag = i;
                    t2.Name = "t2";
                    t2.TextChanged += new TextChangedEventHandler(TextBox_TextChanged);
                    if (!string.IsNullOrEmpty(e.Result[i].SRi)) t2.Text = e.Result[i].SRi;
                    t2.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    t2.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    t2.Width = 60;
                    t2.Height = 25;
                    Binding b3 = new Binding();
                    b3.Source = e.Result[i];
                    b3.Path = new PropertyPath("SRi");
                    b3.Mode = BindingMode.TwoWay;
                    BindingOperations.SetBinding(t2, TextBox.TextProperty, b3);
                    s.Children.Add(t2);
                    



                    TextBlock tb4 = new TextBlock();
                    tb4.Text = "至";
                    tb4.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    tb4.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    s.Children.Add(tb4);

                    TextBox t3 = new TextBox();
                    if (!string.IsNullOrEmpty(e.Result[i].ENian)) t3.Text = e.Result[i].ENian;
                    t3.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    t3.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    t3.Width = 60;
                    t3.Height = 25;
                    Binding b4 = new Binding();
                    b4.Source = e.Result[i];
                    b4.Path = new PropertyPath("ENian");
                    b4.Mode = BindingMode.TwoWay;
                    BindingOperations.SetBinding(t3, TextBox.TextProperty, b4);
                    s.Children.Add(t3);
                    
                    TextBlock tb5 = new TextBlock();
                    tb5.Text = "年";
                    tb5.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    tb5.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    s.Children.Add(tb5);

                    TextBox t4 = new TextBox();
                    if (!string.IsNullOrEmpty(e.Result[i].EYue)) t4.Text = e.Result[i].EYue;
                    t4.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    t4.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    t4.Width = 60;
                    t4.Height = 25;
                    Binding b5 = new Binding();
                    b5.Source = e.Result[i];
                    b5.Path = new PropertyPath("EYue");
                    b5.Mode = BindingMode.TwoWay;
                    BindingOperations.SetBinding(t4, TextBox.TextProperty, b5);
                    s.Children.Add(t4);
                    
                    TextBlock tb6 = new TextBlock();
                    tb6.Text = "月";
                    tb6.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    tb6.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    s.Children.Add(tb6);

                    TextBox t5 = new TextBox();
                    if (!string.IsNullOrEmpty(e.Result[i].ERi)) t5.Text = e.Result[i].ERi;
                    t5.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    t5.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    t5.Width = 60;
                    t5.Height = 25;
                    Binding b6 = new Binding();
                    b6.Source = e.Result[i];
                    b6.Path = new PropertyPath("ERi");
                    b6.Mode = BindingMode.TwoWay;
                    BindingOperations.SetBinding(t5, TextBox.TextProperty, b6);
                    s.Children.Add(t5);
                   

                    TextBlock tb7 = new TextBlock();
                    tb7.Text = "公示" + e.Result[i].EmpName + "同志转任" + e.Result[i].Duty;
                    tb7.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    tb7.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    s.Children.Add(tb7);

                    TextBlock tb8 = new TextBlock();
                    tb8.Margin = new Thickness(100, 3, 3, 3);
                    tb8.Text = "    处理意见如下：";
                    tb8.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    tb8.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;

                    TextBox t8 = new TextBox();
                    t8.Width = 250;
                    t8.Height = 25;
                    
                    if (!string.IsNullOrEmpty(e.Result[i].ChuLiQingKuang)) t8.Text = e.Result[i].ChuLiQingKuang;
                    t8.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    t8.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    Binding b9 = new Binding();
                    b9.Source = e.Result[i];
                    b9.Path = new PropertyPath("ChuLiQingKuang");
                    b9.Mode = BindingMode.TwoWay;
                    BindingOperations.SetBinding(t8, TextBox.TextProperty, b9);


                    StackPanel sp = new StackPanel();
                    sp.Orientation = Orientation.Horizontal;
                    sp.Visibility = System.Windows.Visibility.Collapsed;
                    sp.Children.Add(tb8);
                    sp.Children.Add(t8);

                    ComboBox cb = new ComboBox();
                    cb.Width = 150;
                    cb.Height = 25;
                    cb.Tag = e.Result[i];
                    cb.SelectionChanged += new SelectionChangedEventHandler(cb_SelectionChanged);
                    ComboBoxItem ci = new ComboBoxItem();
                    ci.Content = "未收到情况反映";
                    ComboBoxItem ci1 = new ComboBoxItem();
                    ci1.Content = "有情况反映";
                    cb.Items.Add(ci);
                    cb.Items.Add(ci1);

                    dic.Add(cb, sp);

                    if (!string.IsNullOrEmpty(e.Result[i].IsFanYing))
                    {
                        if (e.Result[i].IsFanYing == "未收到情况反映")
                        {
                            cb.SelectedIndex = 0;
                        }
                        else
                        {
                            cb.SelectedIndex = 1;
                        }
                    }
                    cb.Margin = new Thickness(3, 3, 3, 3);
                    //Binding b = new Binding();
                    //b.Source = e.Result[i];
                    //b.Path = new PropertyPath("IsFanYing");
                    //b.Mode = BindingMode.TwoWay;
                    //BindingOperations.SetBinding(cb, ComboBox.SelectedItemProperty, b);
                    s.Children.Add(cb);

                    
                    

                    stackpanel.Children.Add(s);
                    stackpanel.Children.Add(sp);
                }
            }
            else OKButton.IsEnabled = false;
        }

        private string pattern = @"^[0-9]+([.]{1}[0-9]+){0,1}$";
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            Match m = Regex.Match(((TextBox)sender).Text, pattern);   // 匹配正则表达式
            if (!string.IsNullOrEmpty(((TextBox)sender).Text))
            {
                if (!m.Success)   // 输入的不是数字
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("只能输入数字！");
                    ((TextBox)sender).Text = "";
                    return;
                    //((TextBox)sender).Text = param1;   // textBox内容不变

                    // 将光标定位到文本框的最后
                    //((TextBox)sender).SelectionStart = ((TextBox)sender).Text.Length;
                }
                if(((TextBox)sender).Name == "t2")
                {
                    var ii = models[Convert.ToInt32(((TextBox) sender).Tag)];
                    try
                    {
                        if(!string.IsNullOrEmpty(ii.SNian) && !string.IsNullOrEmpty(ii.SYUe) && !string.IsNullOrEmpty(ii.SRi))
                        {
                            DateTime dt = Convert.ToDateTime(ii.SNian + "-" + ii.SYUe + "-" + ii.SRi);
                            ii.ENian = dt.AddDays(7).Year.ToString();
                            ii.EYue = dt.AddDays(7).Month.ToString();
                            ii.ERi = dt.AddDays(7).Day.ToString();
                        }
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
            

        }

        private Dictionary<ComboBox, StackPanel> dic = new Dictionary<ComboBox,StackPanel>();
        void cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((RenQianGongShi)((ComboBox)sender).Tag).IsFanYing = ((System.Windows.Controls.ContentControl)
                                                                  (((ComboBoxItem)((ComboBox)sender).SelectedItem))).
                Content.
                ToString();
            if (dic.Keys.Contains(((ComboBox)sender)))
            if(((ComboBox)sender).SelectedIndex == 1)
            {
                dic[((ComboBox) sender)].Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                dic[((ComboBox)sender)].Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private int c = 0;
        private string error = "";
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            foreach (RenQianGongShi i in models)
            {
                if(string.IsNullOrEmpty(i.SNian) || string.IsNullOrEmpty(i.SYUe) || string.IsNullOrEmpty(i.SYUe)
                    ||string.IsNullOrEmpty(i.ENian)||string.IsNullOrEmpty(i.EYue)||string.IsNullOrEmpty(i.ERi))
                {
                    MessageBox.Show("请正确填写年月日信息！");
                    return;
                }
            }
            //foreach (RenQianGongShi i in models)
            //{
            //    if (Convert.ToDateTime(i.SNian + "-" + i.SYUe + "-" + i.SRi).AddDays(7) != Convert.ToDateTime(i.ENian + "-" + i.EYue + "-" + i.ERi))
            //    {
            //        error += "\r\n" + i.EmpName + "同志转任公示时间有误！";
            //    }
            //}
            if (MessageBox.Show(error + "\r\n确定上报会议结果吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                
                foreach (RenQianGongShi i in models)
                {
                    if (i.IsFanYing == "未收到情况反映") i.ChuLiQingKuang = "";
                    
                    Client.InsertRenQianGongShiCompleted -= new EventHandler<InsertRenQianGongShiCompletedEventArgs>(Client_InsertRenQianGongShiCompleted);
                    Client.InsertRenQianGongShiCompleted += new EventHandler<InsertRenQianGongShiCompletedEventArgs>(Client_InsertRenQianGongShiCompleted);
                    Client.InsertRenQianGongShiAsync(i);
                    c += 1;
                }
            }
        }

        void Client_InsertRenQianGongShiCompleted(object sender, InsertRenQianGongShiCompletedEventArgs e)
        {
            if (c == models.Count)
            {
                c = c - 1;
                SilverlightMessageBoxLibrary.Message.InfoMessage("上报成功！");
                shenqing.IsEnabled = true;
            }
        }

        private void shenqing_Click(object sender, RoutedEventArgs e)
        {
            string ss = "";
            if (MessageBox.Show("确定申请转任吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                //foreach (RenQianGongShi i in models)
                //{
                //    if (i.IsShenQingZhuanRen == false)
                //    {
                //        SilverlightMessageBoxLibrary.Message.ErrorMessage("还未提交转任申请，不可确认转任！");
                //        return;
                //    }
                //}
                foreach(RenQianGongShi i in models)
                {
                    ss += i.PublicOfficialRecordID.ToString() + ",";
                }
                Client.QueRenShenQingZhuanRenCompleted -= new EventHandler<QueRenShenQingZhuanRenCompletedEventArgs>(Client_QueRenShenQingZhuanRenCompleted);
                Client.QueRenShenQingZhuanRenCompleted += new EventHandler<QueRenShenQingZhuanRenCompletedEventArgs>(Client_QueRenShenQingZhuanRenCompleted);
                Client.QueRenShenQingZhuanRenAsync(ss.Substring(0, ss.Length - 1));
            }
        }

        void Client_QueRenShenQingZhuanRenCompleted(object sender, QueRenShenQingZhuanRenCompletedEventArgs e)
        {
            if(e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("申请成功！");
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请完整填写任前管理信息！"); 
            }
        }

        private void queren_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确定转任吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                foreach (RenQianGongShi i in models)
                {
                    if (i.IsZhuanRen == true)
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("已转任，不可重复操作！");
                        return;
                    }
                }
                foreach (RenQianGongShi i in models)
                {
                    Client.QueRenZhuanRenCompleted -= new EventHandler<QueRenZhuanRenCompletedEventArgs>(Client_QueRenZhuanRenCompleted);
                    Client.QueRenZhuanRenCompleted += new EventHandler<QueRenZhuanRenCompletedEventArgs>(Client_QueRenZhuanRenCompleted);
                    Client.QueRenZhuanRenAsync(i.PublicOfficialRecordID);
                    cd += 1;
                }
            }
        }

        private int cd = 0;
        void Client_QueRenZhuanRenCompleted(object sender, QueRenZhuanRenCompletedEventArgs e)
        {
            if (cd == models.Count)
            {
                cd = cd - 1;
                SilverlightMessageBoxLibrary.Message.InfoMessage("确认转任成功！");
            }
        }
    }
}
